package com.deparse.dpframe.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hsy on 2016/4/21.
 */
public class DPRegUtil {

    public static String regReplace(String src, String pattern, String replaceWith) {
        Pattern pat = Pattern.compile(pattern);
        Matcher mat = pat.matcher(src);
        return mat.replaceAll(replaceWith);
    }

    public static String[] regSplit(String src, String splitPattern) {
        Pattern pat = Pattern.compile(splitPattern);
        return pat.split(src);
    }

    public static String[] regFind(String src, String pattern) {
        Pattern pat = Pattern.compile(pattern);
        Matcher mat = pat.matcher(src);
        String[] rt = null;
        if (mat.find() && mat.groupCount() > 0) {
            rt = new String[mat.groupCount()];
            for (int i = 0; i < rt.length; i++) {
                rt[i] = mat.group(i);
            }
        }
        return rt;
    }

    public static boolean regContains(String src, String pattern) {
        Pattern pat = Pattern.compile(pattern);
        Matcher mat = pat.matcher(src);
        return mat.find();
    }

    public static boolean Matches(String s, String reg) {
        return s.matches(reg);
    }

    public static boolean isCellPhone(String s) {
        String pattern = "^\\d{11}$";
        return Matches(s, pattern);
    }

    public static boolean isEmail(String s) {
        String pattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        return Matches(s, pattern);
    }

    public static boolean isUrl(String s) {
        String pattern = "^[a-zA-z]+://[^\\s]*$";
        return Matches(s, pattern);
    }

    public static boolean isAccountInvalid(String s) {
        String pattern = "^[_a-zA-Z][a-zA-Z0-9_]{5,15}$";
        return Matches(s, pattern);
    }

    public static boolean isLandLine(String s) {
        String pattern = "^((\\d{3,4}\\-)|)\\d{7,8}(|([-\\u8f6c]{1}\\d{1,5}))$";
        return Matches(s, pattern);
    }


    //模糊匹配
    interface MatchHander{
        boolean compare(int a,int b);
    }

    /**
     * 百分之多少之内匹配错误可以接受
     * a与ab匹配为百分之50的错误率。
     * @param percent 设置匹配百分比
     * @param src 字符串1
     * @param dest 字符串2
     * @param hander 匹配规则
     * @return
     */
    public static boolean match(double percent,String src,String dest,MatchHander hander){
        char[] csrc = src.toCharArray();
        char[] cdest = dest.toCharArray();
        double score = 0;
        score = cal(csrc,0,cdest,0,hander);
        int max = csrc.length>cdest.length?csrc.length:cdest.length;
        System.out.println("最小匹配百分比："+percent+"，成功匹配百分比："+score / max);
        return score / max > percent;
    }

    /**
     * 几个错误的字符可以接受
     * a与ab为1个字符错误可以接受
     * @param src 字符串1
     * @param dest 字符串2
     * @param hander 匹配规则
     * @return
     */
    public static boolean match(int errorNum,String src,String dest,MatchHander hander){
        char[] csrc = src.toCharArray();
        char[] cdest = dest.toCharArray();
        int score = 0;
        score = cal(csrc,0,cdest,0,hander);
        int max = csrc.length>cdest.length?csrc.length:cdest.length;
        System.out.println("可以接受错误数："+errorNum+"，发现错误数："+(max - score) );
        return max - score  <= errorNum;
    }
    /**
     * 2个字符串75%匹配成功返回true
     * @param percent 设置匹配百分比
     * @param src
     * @param dest
     * @return
     */
    public static boolean match(double percent,String src,String dest){
        return match(percent,src,dest,new MatchHander(){

            @Override
            public boolean compare(int a, int b) {
                return a==b;
            }});
    }
    /**
     * 2个字符串错几个字符可以接受
     * @param errorNum
     * @param src
     * @param dest
     * @return
     */
    public static boolean match(int errorNum,String src,String dest){
        return match(errorNum,src,dest,new MatchHander(){

            @Override
            public boolean compare(int a, int b) {
                return a==b;
            }});
    }

    /**
     * 使用递归方法匹配字符串
     * @param csrc
     * @param i
     * @param cdest
     * @param j
     * @param hander
     * @return
     */
    private static int cal(char[] csrc, int i, char[] cdest, int j, MatchHander hander) {
        int score = 0;
        if(i >= csrc.length || j >= cdest.length)
            return 0;
        boolean ismatch = hander.compare(csrc[i], cdest[j]);
        if(ismatch){
            score++;
            if(i+1<csrc.length && j+1<cdest.length)
                score +=  cal(csrc, i+1, cdest, j+1, hander) ;
        }else{
            int temp1 = 0;
            int temp2 = 0;
            int temp3 = 0;
            temp1 +=  cal(csrc, i, cdest, j+1, hander) ;
            temp2 +=  cal(csrc, i+1, cdest, j, hander) ;
            temp3 +=  cal(csrc, i+1, cdest, j+1, hander) ;
            int temp4 = Math.max(temp1, temp2);
            score += Math.max(temp3, temp4);
        }
        return score;
    }
}
